package com.ebupt.migu.music.bannertwo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.music.bannertwo.entity.BannerTwoOperate;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoOperateService;
import com.ebupt.migu.music.common.entity.ResultObject;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;


@RestController
@RequestMapping("/BannerTwoOperate")
@Slf4j
public class BannerTwoOperateController{
	@Autowired
	private IBannerTwoOperateService bannerTwoOperateService;
	
	/**
	 * 分页列表查询
	 *
	 * @param bannerTwoOperate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "banner_two_operate-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<BannerTwoOperate>> queryPageList(BannerTwoOperate bannerTwoOperate,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<BannerTwoOperate> queryWrapper = QueryGenerator.initQueryWrapper(bannerTwoOperate, req.getParameterMap());
//		Page<BannerTwoOperate> page = new Page<BannerTwoOperate>(pageNo, pageSize);
//		IPage<BannerTwoOperate> pageList = bannerTwoOperateService.page(page, queryWrapper);
//		return ResultObject.ok(pageList);
//	}
	
	/**
	 *   添加
	 *
	 * @param bannerTwoOperate
	 * @return
	 */
	//@RequiresPermissions("org.jeecg.modules.demo:banner_two_operate:add")
	@PostMapping(value = "/add")
	public ResultObject<String> add(@RequestBody BannerTwoOperate bannerTwoOperate) {
		bannerTwoOperateService.save(bannerTwoOperate);
		return ResultObject.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param bannerTwoOperate
	 * @return
	 */
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public ResultObject<String> edit(@RequestBody BannerTwoOperate bannerTwoOperate) {
		bannerTwoOperateService.updateById(bannerTwoOperate);
		return ResultObject.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public ResultObject<String> delete(@RequestParam(name="id",required=true) String id) {
		bannerTwoOperateService.removeById(id);
		return ResultObject.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public ResultObject<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bannerTwoOperateService.removeByIds(Arrays.asList(ids.split(",")));
		return ResultObject.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public ResultObject<?> queryById(@RequestParam(name="id",required=true) String id) {
		BannerTwoOperate bannerTwoOperate = bannerTwoOperateService.getById(id);
		if(bannerTwoOperate==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(bannerTwoOperate);
	}

	@GetMapping(value = "/queryByBId")
	public ResultObject<?> queryByBId(@RequestParam(name="id",required=true) String id) {
		BannerTwoOperate bannerTwoOperate = bannerTwoOperateService.getOne(new QueryWrapper<BannerTwoOperate>().lambda().eq(BannerTwoOperate::getBannerId,id).eq(BannerTwoOperate::getOperateType,"1"));
		if(bannerTwoOperate==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(bannerTwoOperate);
	}


}
